<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/11
 * Time: 15:10
 */

namespace Biz\model\activity;

use Biz\tools\BizUrl;
use Doraemon\tools\ImgUrl;

class Page extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'page';

    const _SUB_TABLE = false;

    // 状态为正常
    const STATUS_NORMAL = 1;
    // 状态为下架
    const STATUS_OFFLINE = 2;
    // 状态为被删除
    const STATUS_DELETE = 9;

    const PAGE_TYPE_MALL = 1;
    const PAGE_TYPE_AD = 2;
    const PAGE_TYPE_SPECIAL = 3;

    const PAGE_TYPE_DESC = [
        self::PAGE_TYPE_MALL => '商城',
        self::PAGE_TYPE_AD => '广告',
        self::PAGE_TYPE_SPECIAL => '住友津贴',
    ];

    public static function getValidPagesByPageIds($pageIds, $date = "")
    {
        if (!$date) {
            $date = date('Y-m-d H:i:s');
        }
        $pageList = self::select("*", [
            'id' => $pageIds,
            'status' => self::STATUS_NORMAL,
            'start_time[<=]' => $date,
            'end_time[>=]' => $date
        ]);
        if ($pageList) {
            foreach ($pageList as &$page) {
                $page = self::formatPageInfo($page);
            }
        }
        return $pageList;
    }

    public static function getListAndTotal($where, $limit)
    {
        if (!isset($where['ORDER'])) {
            $where['ORDER'] = [
                'sort',
                'id' => 'DESC'
            ];
        }

        $where['LIMIT'] = $limit;

        $list = self::select("*", $where);
        if ($list) {
            foreach ($list as &$page) {
                $page = self::formatPageInfo($page);
            }
        }
        $total = self::count('*', $where['AND']);
        return compact('list', 'total');
    }

    public static function getList($where)
    {
        if (!isset($where['ORDER'])) {
            $where['ORDER'] = [
                'sort',
                'id' => 'DESC'
            ];
        }
        $pageList = self::select("*", $where);
        if ($pageList) {
            foreach ($pageList as &$page) {
                $page = self::formatPageInfo($page);
            }
        }
        return $pageList;
    }

    public static function getPageInfoById($pageId)
    {
        $pageinfo = self::get("*", ['id' => $pageId]);
        if ($pageinfo) {
            return self::formatPageInfo($pageinfo);
        } else {
            return [];
        }
    }

    private static function formatPageInfo($pageInfo)
    {
        $pageInfo['url'] = BizUrl::h5ActivityPage($pageInfo['id']);
        $pageInfo['tags'] = str_replace("#", '', $pageInfo['tags']);
        $pageInfo['tags_array'] = explode(",", $pageInfo['tags']);
        $pageInfo['relation_id_array'] = explode(",", $pageInfo['relation_id']);
        $pageInfo['cover_img_big_url'] = $pageInfo['cover_img_big'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pageInfo['cover_img_big'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        $pageInfo['cover_img_small_url'] = $pageInfo['cover_img_small'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pageInfo['cover_img_small'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        $pageInfo['icon_url'] = $pageInfo['icon'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pageInfo['icon'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        $pageInfo['share_img_url'] = $pageInfo['share_img'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pageInfo['share_img'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        $pageInfo['goods_icon_url'] = $pageInfo['goods_icon'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pageInfo['goods_icon'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        $pageInfo['goods_banner_url'] = $pageInfo['goods_banner'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pageInfo['goods_banner'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        return $pageInfo;
    }

    public static function savePageInfo($pageInfo, $id = 0)
    {
        if ($id) {
            $pageInfo['edittime'] = date("Y-m-d H:i:s");
            if (self::update($pageInfo, ["id" => $id])->rowcount()) {
                return $id;
            } else {
                return false;
            }
        } else {
            $pageInfo['status'] = self::STATUS_NORMAL;
            $pageInfo['addtime'] = date("Y-m-d H:i:s");
            if (self::insert($pageInfo)->rowcount()) {
                return self::id();
            } else {
                return false;
            }
        }
    }

    public static function changePageStatusById($id, $status)
    {
        if (self::update(['status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }

    public static function changeAppStatusById($id, $status)
    {
        if (self::update(['app_status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }

    public static function changeMiniStatusById($id, $status)
    {
        if (self::update(['mini_status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }

    public static function getBasicInfoByAdmin($id, $field = 'title')
    {
        return self::get($field, ['id' => $id]);
    }

}